package geppetto.cat.analysis;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.cat.models.AgreementHMM;

import java.io.IOException;



public class PrintPosteriorsProjection {
	AgreementHMM mhmm, mhmmb;

	Alignment forward, backward;

	public static void main(String[] args) throws IOException {
		PrintPosteriorsProjection pp = new PrintPosteriorsProjection();
		String corpusDescription = args[0];
		int size = Integer.parseInt(args[1]);
		int maxSentenceLen = Integer.parseInt(args[2]);
		String modelDir = args[3];
		String modelName = args[4];
		String decodingType = args[5];
		System.out.println("Corpus " + corpusDescription);
		System.out.println("Size " + size);
		System.out.println("Max Sentence Len " + maxSentenceLen);
		System.out.println("ModelDir " + modelDir);
		System.out.println("Model Name " + modelName);
		System.out.println("Decoding type " + decodingType);
		System.out.println("---");
		BilingualCorpus forwardCorpus = BilingualCorpus
				.getCorpusFromFileDescription(corpusDescription, size,
						maxSentenceLen);
		BilingualCorpus backwardCorpus = forwardCorpus.reverse();
		modelDir = modelDir + "/" + modelName + "/" + forwardCorpus.getName()
				+ "/" + size + "/model/";
		System.out.println("Loading Model");
		if (modelName.equalsIgnoreCase("agreement")) {
			pp.mhmm = AgreementHMM.loadModel(forwardCorpus, backwardCorpus,
					modelDir + "/MHMM/");
			pp.mhmmb = (pp.mhmm).getBackwardModel();
		} else {
			System.out.println("Unknown Model: " + modelName);
			System.exit(1);
		}
		System.out.println("Finished Loading Model");
		float tresh = 0, tresh2 = 0;

		if (decodingType.equalsIgnoreCase("post-aer")) {
			tresh = pp.mhmm.tuneTreshholdAER(BilingualCorpus.DEV_CORPUS,false);
			tresh2 = pp.mhmmb.tuneTreshholdAER(BilingualCorpus.DEV_CORPUS,false);
		}
		System.out.println("\\documentclass{article} ");
		System.out.println("\\begin{document} ");
		int numberOfGold = forwardCorpus.getGold().size();
		Alignment forwardA = null, backwardA = null;
		for (int i = 0; i < numberOfGold; i++) {
			forwardA = null;
			backwardA = null;
			Alignment gold = forwardCorpus.getGold().get(i);
			System.out.println(i + " " + gold._foreignLen + " "
					+ gold._sourceLen);
			if (gold._foreignLen < 10 && gold._sourceLen < 10) { // Good for
																	// printing
				System.out.println("Sentence nr  " + i);
				if (decodingType.equalsIgnoreCase("viter")) {
					forwardA = pp.mhmm.viterbiAlignment(i,
							BilingualCorpus.TEST_CORPUS,false,null);
					backwardA = pp.mhmmb.viterbiAlignment(i,
							BilingualCorpus.TEST_CORPUS,false,null);

				} else if (decodingType.equalsIgnoreCase("post-aer")) {
					forwardA = pp.mhmm.posteriorDecodingAlignment(i,
							BilingualCorpus.TEST_CORPUS, tresh,false,null);
					backwardA = pp.mhmmb.posteriorDecodingAlignment(i,
							BilingualCorpus.TEST_CORPUS, tresh2,false,null);
				} else {
					System.out.println("Invalid decoding ");
					System.exit(1);
				}
			}
			if (forwardA != null && backwardA != null) {
			//	pp.mhmm.printPosteriorAlignment(forwardA, backwardA, i,
		//				BilingualCorpus.TEST_CORPUS, forwardCorpus,
		//				backwardCorpus);
			}
		}
		System.out.println();
		System.out.println();
		System.out.println("\\end{document} ");

	}
}
